package my.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.bean.Order;
import my.dao.IOrderDao;
import my.dao.lmpl.IOrderDaoImpl;
import my.dbUtil.DBUtils;
import my.service.IOrderService;

public class IOrderServiceImpl implements IOrderService {

	@Override
	public int addOrder(Order order) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			record = orderDao.addOrder(order);
			return record;
		} catch (SQLException e) {
			System.err.println("新增失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteOrderById(int id) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			record = orderDao.deleteOrderById(id);
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Order> findAll() {
		Connection conn = null;
		List<Order> list = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			list = orderDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Order> findByUserid(int id) {
		Connection conn = null;
		List<Order> list = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			list = orderDao.findByUserid(id);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Order> findByTeaId(int id) {
		Connection conn = null;
		List<Order> list = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			list = orderDao.findByTeaId(id);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Order> findByTeaIdAndUserID(int uId, int tId) {
		Connection conn = null;
		List<Order> list = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			list = orderDao.findByTeaIdAndUserID(uId, tId);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Order> findByTeaType(int type) {
		Connection conn = null;
		List<Order> list = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			list = orderDao.findByTeaType(type);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Order findByOrderId(int id) {
		Connection conn = null;
		Order order = null;
		try {
			conn = DBUtils.getConnection();
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			order = orderDao.findByOrderId(id);
			return order;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

}
